package mwa.cs.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import mwa.cs.dao.*;
import mwa.cs.database.Database;
import mwa.cs.model.SMApplicationLog;

public class GenerateMenu {
	String vaIconRootClass = "\"fa fa-briefcase\"";
	String vaIconFolderClass = "\"fa fa-folder\"";
	String vaIconLeafClass = "\"fa fa-coffee\"";
	String vaIconArrow = "<span class=\"arrow\"></span>";
	String vaMenuHtml = "";
	String vaAuthority = "";
	int vnUserId = 0;
	String vaUserName = "";
	String vaPageName = "";
	String vaSrvlName = "";
	String vaMenuID = "";
	String vaUrlAddress = "";
	String vaAgentBrowser = "";
	SMApplicationLog voLog;
	SMApplicationLogDAO voSMApplicationLog;
	Database db = new Database();
	
	public String getMenuHtml() {

		String[] temp = vaPageName.split("/");
		vaPageName = temp[(temp.length-2)]+"/"+temp[(temp.length-1)];
		String[] temp2 = temp[(temp.length-1)].split(".jsp");
		vaSrvlName = temp[(temp.length-2)]+"/"+temp2[(temp2.length-1)]+"Srvl";
		MenuDAO mn = new MenuDAO();
		List<Map<String, Object>> menu = mn.getMenu(vnUserId);
		Map<String, Object> menuActive = mn.getMenuActive(vaPageName,vaMenuID,vaSrvlName);
		String vaMainActive = "";
		String vaParent1 = "";
		String vaParent2 = "";

		if(!temp[(temp.length-1)].equals("news.jsp")){
			
			vaMainActive = menuActive.get("MAIN_ACIVE").toString();
			vaParent1 = menuActive.get("PARENT_ACTIVE1").toString();
			vaParent2 = menuActive.get("PARENT_ACTIVE2").toString();
		}

		List<Object> voStack = new ArrayList<Object>();
		String vaMenuUrlPath = "/CS/";
		String vaUrlMenu = "";
		String vaTempActv = "";
		
		for(int i=0; i<menu.size(); i++){
			try{

				if(menu.get(i).get("MENU_PARENT").equals("Y")){
					
					if(menu.get(i).get("MENU_ROUTE_ID").equals(vaMainActive) || 
							menu.get(i).get("MENU_ROUTE_ID").equals(vaParent1) || 
							menu.get(i).get("MENU_ROUTE_ID").equals(vaParent2)){
						vaTempActv = "class=\"active\" ";
					}else{
						vaTempActv = "";
					}
					
					if(menu.get(i).get("MENU_PARENT_ID").equals(menu.get(i).get("MENU_ID"))){
						if(voStack.size()>0){
							for(int a = voStack.size(); a > 0; a--){
								vaMenuHtml = vaMenuHtml+"</ul>";
								vaMenuHtml = vaMenuHtml+"</li>";
								voStack.remove(a-1);								
							}
						}
						vaMenuHtml = vaMenuHtml+"<li id=\""+menu.get(i).get("MENU_ROUTE_ID")+"\" "+vaTempActv+"><a href=\"javascript:void()\"><i class="+vaIconRootClass+"></i> "+menu.get(i).get("MENU_NAME")+vaIconArrow+"</a>";
					}else{
						vaMenuHtml = vaMenuHtml+"<li id=\""+menu.get(i).get("MENU_ROUTE_ID")+"\" "+vaTempActv+"><a href=\"javascript:void()\"><i class="+vaIconFolderClass+"></i> "+menu.get(i).get("MENU_NAME")+vaIconArrow+"</a>";
					}
					vaMenuHtml = vaMenuHtml+"<ul class=\"sub-menu\">";
					voStack.add(menu.get(i).get("MENU_ID"));
				}else if(menu.get(i).get("MENU_PARENT").equals("N") && menu.get(i).get("MENU_PARENT_ID").equals(menu.get(i).get("MENU_ID"))){
					if(voStack.size()>0){
						for(int a = voStack.size(); a > 0; a--){
							vaMenuHtml = vaMenuHtml+"</ul>";
							vaMenuHtml = vaMenuHtml+"</li>";
							voStack.remove(a-1);								
						}
					}
					
					if(menu.get(i).get("MENU_ROUTE_ID").equals(vaMainActive) || 
							menu.get(i).get("MENU_ROUTE_ID").equals(vaParent1) || 
							menu.get(i).get("MENU_ROUTE_ID").equals(vaParent2)){
						vaTempActv = "class=\"active\" ";
					}else{
						vaTempActv = "";
					}
					vaMenuHtml = vaMenuHtml+"<li id=\""+menu.get(i).get("ROUTE_ID")+"\" "+vaTempActv+"><a href=\"javascript:void()\"><i class="+vaIconLeafClass+"></i> "+menu.get(i).get("MENU_NAME")+"</a>";
					vaMenuHtml = vaMenuHtml+"<ul class=\"sub-menu\">";
					voStack.add(menu.get(i).get("MENU_ID"));	
				}else{
					if(menu.get(i).get("MENU_ROUTE_ID").equals(vaMainActive) || 
							menu.get(i).get("MENU_ROUTE_ID").equals(vaParent1) || 
							menu.get(i).get("MENU_ROUTE_ID").equals(vaParent2)){
						vaTempActv = "class=\"active\" ";
					}else{
						vaTempActv = "";
					}
					vaUrlMenu = "<li id=\""+menu.get(i).get("MENU_ROUTE_ID")+"\" "+vaTempActv+"><a href=\""+ vaMenuUrlPath + "util/ApplicationLog?menuID="+menu.get(i).get("MENU_ID")+
							"&menuName="+menu.get(i).get("MENU_NAME")+"&menuURL="+menu.get(i).get("MENU_URL")+"\" "+vaTempActv+"><i class="+vaIconLeafClass+"></i> "+menu.get(i).get("MENU_NAME")+"</a>"+"</li>";

					if(voStack.size()>0){
						if(voStack.get( voStack.size()-1).equals(menu.get(i).get("MENU_PARENT_ID")) ){
							if(menu.size()>i+1){
								if(voStack.get( voStack.size()-1).equals(menu.get(i+1).get("MENU_PARENT_ID")) ){
									vaMenuHtml = vaMenuHtml+vaUrlMenu;
								}else if(menu.get(i+1).get("MENU_PARENT_ID").equals("")){
									vaMenuHtml = vaMenuHtml+"</ul>";
									vaMenuHtml = vaMenuHtml+"</li>";
									voStack.remove(voStack.size()-1);
								}else{
									vaMenuHtml = vaMenuHtml+vaUrlMenu;
									vaMenuHtml = vaMenuHtml+"</ul>";
									vaMenuHtml = vaMenuHtml+"</li>";
									voStack.remove(voStack.size()-1);
								}
							}else{
								vaMenuHtml = vaMenuHtml+vaUrlMenu;
								vaMenuHtml = vaMenuHtml+"</ul>";
								vaMenuHtml = vaMenuHtml+"</li>";
								voStack.remove(voStack.size()-1);
							}
						}else{ 
							vaMenuHtml = vaMenuHtml+vaUrlMenu;
						}
					}else{
						vaMenuHtml = vaMenuHtml+vaUrlMenu;
					}
					
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		if(voStack.size()>0){
			for(int i = voStack.size(); i>0; i--){
				vaMenuHtml = vaMenuHtml+"</ul>";
				vaMenuHtml = vaMenuHtml+"</li>";
			}
		}
		
		vaMenuHtml = vaMenuHtml+"<li><a href='../LogoutSrvl'><i class='fa fa-sign-out'></i> ออกจากระบบ</a></li>";

		db.commit();
		db.close();
		return vaMenuHtml;
	}

	
	public String getAuthority() {
		String[] vaTemp = vaPageName.split("/");
		vaPageName = vaTemp[(vaTemp.length-2)]+"/"+vaTemp[(vaTemp.length-1)];
		String[] temp2 = vaTemp[(vaTemp.length-1)].split(".jsp");
		vaSrvlName = vaTemp[(vaTemp.length-2)]+"/"+temp2[(temp2.length-1)]+"Srvl";
		
		if(vnUserId==0){
			vaAuthority = "<script>window.location.assign(\"../index.jsp\")</script>";
		}else{
			if(!vaTemp[(vaTemp.length-1)].equals("news.jsp")){
				
				UserLoginDAO voUl = new UserLoginDAO(db);
				if(voUl.findAuthority(vnUserId, vaPageName,vaMenuID ,vaSrvlName) == false){
					vaAuthority = "<script>window.location.assign(\"../index.jsp\")</script>";
				}				
			}
		}

		return vaAuthority;
	}


	public void setAuthority(String authority) {
		this.vaAuthority = authority;
	}


	public int getUserId() {
		return vnUserId;
	}



	public void setUserId(int userId) {
		this.vnUserId = userId;
	}



	public String getPageName() {
		return vaPageName;
	}



	public void setPageName(String pageName) {
		this.vaPageName = pageName;
	}



	public String getMenuID() {
		return vaMenuID;
	}



	public void setMenuID(String menuID) {
		this.vaMenuID = menuID;
	}

	

	public String getUserName() {
		return vaUserName;
	}



	public void setUserName(String userName) {
		this.vaUserName = userName;
	}



	public String getUrlAddress() {
		return vaUrlAddress;
	}



	public void setUrlAddress(String urlAddress) {
		this.vaUrlAddress = urlAddress;
	}



	public String getAgentBrowser() {
		return vaAgentBrowser;
	}



	public void setAgentBrowser(String agentBrowser) {
		this.vaAgentBrowser = agentBrowser;
	}

}
